import { usePopupDialogStore } from '@/store/popup-dialog'
import { $t } from '@/i18n'
import { usePayment } from '@/compositions/payment'
import { CONSUME_TYPE, POPUP_DIALOG_TYPE } from '@/constant'

/**
 * 使用支付對話框的鉤子函數。
 *
 * @param {Object} options 選項對象
 * @param {string} options.popupDialogType 彈窗類型，預設為 POPUP_DIALOG_TYPE.CONFIRM
 * @see {@link POPUP_DIALOG_TYPE} 支持的彈窗類型
 * @param {Object} options.popupDialogConfig 彈窗配置，預設為空對象
 * @param {Function} options.popupDialogConfirmAction 彈窗的確認按鈕行為，預設為空函數
 * @param {Object} options.payActionConfig 支付行為配置
 * @param {number} options.payActionConfig.paymentType 支付類型，預設為 CONSUME_TYPE.REWARD
 * @see {@link CONSUME_TYPE} 支持的支付類型
 * @param {number} options.payActionConfig.amount 金額，預設為 0
 * @param {string} options.payActionConfig.userUUID 用戶 UUID，預設為空字符串
 * @param {Object} options.payActionConfig.actions 行為對象
 * @param {Function} options.payActionConfig.actions.onSuccess 成功時的回調函數，預設為空函數
 * @param {Function} options.payActionConfig.actions.onFailure 失敗時的回調函數，預設為空函數
 * @param {Function} options.payActionConfig.actions.onCancel 取消時的回調函數，預設為空函數
 * @param {Function} options.payActionConfig.payloadFactory Request.Payload的工廠函數，預設為空函數
 * @param {Object} options.extraData 額外數據，預設為空對象
 *
 * @returns {Object} 包含 multiple 和 stripe 函數的對象
 */
export function usePaymentDialog({
  popupDialogType = POPUP_DIALOG_TYPE.CONFIRM,
  popupDialogConfig = {},
  payActionConfig = {
    api: {},
    checkApi: {},
    paymentType: CONSUME_TYPE.REWARD,
    amount: null,
    userUUID: '',
    actions: {
      onSuccess: () => {},
      onFailure: () => {},
      onCancel: () => {},
    },
    payloadFactory: () => {},
  },
  extraData = {},
}) {
  const { $popupDialog } = usePopupDialogStore()
  const { pay, payStripe, cancel } = usePayment()
  const { payloadFactory, api, checkApi, paymentType, amount, userUUID, actions } = payActionConfig

  const __progressDialog = {
    title: $t('payment.pending'),
    content: {
      indeterminate: true,
    },
    showClose: false,
    showConfirm: true,
    confirmText: $t('common.cancel'),
    confirm: async () => cancel(),
  }

  const __multiDialogConfig = {
    title: $t('title.choosePayway'),
    confirmText: $t('common.confirm'),
    cancelText: $t('common.cancel'),
    content: {},
    cancel: () => {},
  }

  function multiple() {
    $popupDialog(popupDialogType, {
      ...popupDialogConfig,
      content: { ...popupDialogConfig.content, multiPay: true },
    })
      .next(POPUP_DIALOG_TYPE.EMBED_PAYMENT, {
        ...__multiDialogConfig,
        confirm: __payAction,
      })
      .next(POPUP_DIALOG_TYPE.PROGRESS, __progressDialog)
      .open()
  }

  function single() {
    $popupDialog(POPUP_DIALOG_TYPE.EMBED_PAYMENT, {
      ...__multiDialogConfig,
      confirm: __payAction,
      content: { amount },
    })
      .next(POPUP_DIALOG_TYPE.PROGRESS, __progressDialog)
      .open()
  }

  async function __payAction(data) {
    const __data = await data
    const payload = await payloadFactory(data)

    // 適用於 Stripe 內嵌模塊的支付，傳入的 data 應該要有 setupIntent
    if (__data.intent || __data.paymentMethod) {
      const paymentMethod = __data.intent?.payment_method || __data?.paymentMethod

      const payStripeConfig = {
        data: { ...__data, ...payload },
        paymentType,
        userUUID,
        amount: __data.amount || payload.amount || amount,
        paymentMethod,
        ...actions,
        extraData,
      }

      console.log('payStripeConfig', payStripeConfig)

      await payStripe(payStripeConfig)
    }
    // 適用 Happy 金流的支付，傳入的 data 應該要有 payway
    else {
      payload.pay_type_id = __data.payway

      await pay({
        api,
        checkApi,
        data: payload,
        paymentType,
        newWindow: __data.newWindow,
        userUUID,
        amount: __data.amount || amount,
        ...actions,
        extraData,
      })
    }
  }

  return { multiple, single }
}
